package com.goktech.mapper;

import com.goktech.entity.User;
import com.goktech.entity.borrow;
import com.goktech.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName testBorrowMapper
 * @Description TODO
 * @Author 邱华英
 * @Date 2022/10/3 15:58
 * @Version 1.0
 */

public class testBorrowMapper {
    SqlSession session = null;
    BorrowMapper borrowMapper = null;

    @Before
    public void before() throws IOException {
        session = SqlSessionUtil.getSqlSession();
        borrowMapper = session.getMapper(BorrowMapper.class);
    }

    @After
    public void after(){
        session.close();
    }

    @Test
    public void testinsertBorrow(){
        borrow b = new borrow();
        b.setUserId(6);
        b.setBookId(6);
        b.setBorrowTime(new Date());
        b.setReturnTime(new Date());
        System.out.println(borrowMapper.insertBorrow(b));
    }

    @Test
    public void testdeleteBorrow(){
        borrow b = new borrow();
        b.setUserId(6);
        b.setBookId(6);
        b.setId(944);
        b.setBorrowTime(new Date());
        b.setReturnTime(new Date());
        System.out.println(borrowMapper.deleteBorrow(b));
    }

    @Test
    public void testupdateBorrow(){
        borrow b = new borrow();
        b.setUserId(6);
        b.setBookId(6);
        b.setId(945);
        b.setBorrowTime(new Date());
        b.setReturnTime(new Date());
        b.setXujieTime(new Date());
        System.out.println(borrowMapper.updateBorrow(b));
    }

    @Test
    public void updateBorrow1(){
        borrow b = new borrow();
        b.setReallyTime(new Date());
        b.setIsReturn("true");
        b.setId(945);
        b.setXujieTime(new Date());
        System.out.println(borrowMapper.updateBorrow1(b));
    }

    @Test
    public void testfindByUserId(){
        User u = new User();
        u.setId(6);
        System.out.println(borrowMapper.findByUserId(u,0,5));
    }

    @Test
    public void testfindByAll(){
        System.out.println(borrowMapper.findByAll(0,5));
    }

    @Test
    public void testfindLikeZhanghao(){
        User u = new User();
        u.setZhanghao("%12%");
        System.out.println(borrowMapper.findLikeZhanghao(u));
    }

    @Test
    public void testfindLikeUsername(){
        User u = new User();
        u.setUserName("%2%");
        System.out.println(borrowMapper.findLikeUsername(u));
    }

    @Test
    public void testfindByBorrid(){
        borrow b = new borrow();
        b.setId(945);
        System.out.println(borrowMapper.findByBorrid(b));
    }

    @Test
    public void testfindByBorrow(){
        borrow b = new borrow();
        b.setId(945);
        System.out.println(borrowMapper.findByBorrow(b));
    }

    @Test
    public void testTime() throws ParseException {
        String dataStr = "2022-10-11";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startTime = simpleDateFormat.parse(dataStr);
        Date endTime = new Date();  //获取当前时间
        long s = endTime.getTime()-startTime.getTime();  //endeTime和startTime是两个要计算的时间
        TimeUnit time = TimeUnit.DAYS;
        long days = time.convert(s, TimeUnit.MILLISECONDS);
        System.out.println(days);
    }
}
